package com.lg.atp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lg.atp.dao.NumberInfoDao;
import com.lg.atp.dao.ScheduleJobMapper;
import com.lg.atp.dao.TestTaskCaseDao;
import com.lg.atp.dao.TestTaskDao;
import com.lg.atp.dao.TestTaskDeviceDao;
import com.lg.atp.entity.ScheduleJob;
import com.lg.atp.entity.TblNumberInfo;
import com.lg.atp.entity.TblTestTask;
import com.lg.atp.entity.TblTestTaskCase;
import com.lg.atp.entity.TblTestTaskDevice;
import com.lg.atp.service.ITaskService;

@Service("iTaskService")
public class TaskServiceImpl implements ITaskService{

	@Autowired
	private TestTaskDao testTaskDao;
	@Autowired
	private TestTaskCaseDao testTaskCaseDao;
	@Autowired
	private TestTaskDeviceDao testTaskDeviceDao;
	@Autowired
	private NumberInfoDao numberInfoDao;
	
	@Autowired
	private ScheduleJobMapper scheduleJobMapper;
	
	
	
	private final String taskNumberCode = "TASK";
	private final String taskNumberPrefix = "BT";
	
	@Override
	public List<TblTestTaskCase> selectCaseFromTask(Long taskId) {
		return testTaskCaseDao.selectCaseFromTask(taskId);
	}



	@Override
	public PageInfo<TblTestTask> selectAllTestTask(Map<String,Object> map) {
		TblTestTask task = map.get("task") == null?null:(TblTestTask)map.get("task");
		Integer pageIndex = Integer.valueOf(map.get("pageIndex").toString());
		Integer pageSize = Integer.valueOf(map.get("pageSize").toString());
		PageHelper.startPage(pageIndex, pageSize);
		List<TblTestTask> list =  testTaskDao.selectAllTestTask(task);
		PageInfo<TblTestTask> page = new PageInfo<TblTestTask>(list);
		return page;
	}

	@Transactional
	@Override
	public void saveTestTask(TblTestTask task) {
		task.setTaskNum(generateNumber());
		testTaskDao.saveTestTask(task);
		Long taskId = task.getId();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("testTaskId", taskId);
		String caseIds = task.getTaskCaseIds();
		if(StringUtils.isNotBlank(caseIds)) {
			List<Long> caseIdList = new ArrayList<Long>();
			
			for(String caseId:caseIds.split(",")) {
				caseIdList.add(Long.valueOf(caseId));
			}
			map.put("caseList", caseIdList);
			testTaskCaseDao.saveTaskCase(map);
			
		}
		
		String deviceIds = task.getTaskDeviceIds();
		if(StringUtils.isNotBlank(deviceIds)) {
			List<Long> deviceIdList = new ArrayList<Long>();
			for(String deviceId:deviceIds.split(",")) {
				deviceIdList.add(Long.valueOf(deviceId));
			}
			map.put("deviceList", deviceIdList);
			testTaskDeviceDao.saveTaskDevice(map);
		}
		
		//添加任务时向定时任务表里添加数据
		ScheduleJob job = new ScheduleJob();
		job.setJobName(task.getTaskNum());
		job.setJobStatus(1);
		job.setAuditstatus(1);
		job.setCronexpression(task.getScheduleCron());
		job.setQuartzclass("com.lg.atp.controller.QuartzJob");
		scheduleJobMapper.insertJob(job);
	}

	@Transactional
	@Override
	public void updateTestTask(TblTestTask task) {
		testTaskDao.updateTestTask(task);
		Long taskId = task.getId();
		testTaskCaseDao.delTaskCase(taskId);
		testTaskDeviceDao.delTaskDevice(taskId);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("testTaskId", taskId);
		String caseIds = task.getTaskCaseIds();
		if(StringUtils.isNotBlank(caseIds)) {
			List<Long> caseIdList = new ArrayList<Long>();
			
			for(String caseId:caseIds.split(",")) {
				caseIdList.add(Long.valueOf(caseId));
			}
			map.put("caseList", caseIdList);
			testTaskCaseDao.saveTaskCase(map);
			
		}
		
		String deviceIds = task.getTaskDeviceIds();
		if(StringUtils.isNotBlank(deviceIds)) {
			List<Long> deviceIdList = new ArrayList<Long>();
			for(String deviceId:deviceIds.split(",")) {
				deviceIdList.add(Long.valueOf(deviceId));
			}
			map.put("deviceList", deviceIdList);
			testTaskDeviceDao.saveTaskDevice(map);
		}
	
	}


	@Override
	public List<TblTestTaskDevice> selectDeviceFromTask(Long taskId) {
		return testTaskDeviceDao.selectDeviceFromTask(taskId);
	}



	@Override
	public String generateNumber() {

		String year = DateFormatUtils.format(new Date(), "yyyy");
		TblNumberInfo number = new TblNumberInfo();
		number.setNumberCode(taskNumberCode);
		Long maxNum = numberInfoDao.selectMaxNum(number);
		if(maxNum == null){
			number.setCurrentNumber(1l);
			numberInfoDao.saveNum(number);
			return taskNumberPrefix+"-"+year+"-"+String.format("%06d", 1);
		}else{
			Long newNum = maxNum+1;
			number.setCurrentNumber(newNum);
			numberInfoDao.updateNum(number);
			int randomInt =(int)(Math.random()*1000);
			return taskNumberPrefix+"-"+year+"-"+String.format("%06d", newNum);
		}
	
	}



	@Override
	public TblTestTask getTaskById(Long taskId) {
		return testTaskDao.getTaskById(taskId);
	}

}
